﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TSGS02 : ViewBase
    {
        #region ==GSART===========
        public static readonly DependencyProperty GSARTProperty = DependencyProperty.Register("GSART",
            typeof(string), typeof(TSGS02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGSARTChanged));
        public string GSART
        {
            get
            {
                return (string)GetValue(GSARTProperty);
            }
            set
            {
                SetValue(GSARTProperty, value); 
                Locate();
            }
        }
        private static void OnGSARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSGS02;
            if (src != null)
            {
                src.GSART = (string)e.NewValue;
            }
        }
        #endregion

   
        #region ==dwGS===========
        public static readonly DependencyProperty dwGSProperty = DependencyProperty.Register("dwGS",
            typeof(DataRow), typeof(TSGS02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwGSChanged));
        public DataRow dwGS
        {
            get
            {
                return (DataRow)GetValue(dwGSProperty);
            }
            set
            {
                SetValue(dwGSProperty, value); 
            }
        }
        private static void OndwGSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSGS02;
            if (src != null)
            {
                src.dwGS = (DataRow)e.NewValue;
            }
        }
        #endregion
 
         
        public TSGS02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGSDAT();
            DataContext = MV;
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwGS = dwMaster;
            if (dwGS != null)
            {
                KEY = "G"+dwGS["GSART"].ToString() + dwGS["GSVAR"].ToString();
                
            }
            else
            {
                KEY = "";
            }
        }
         
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            Load();
        }
        public override void OnKRS()
        {
            base.OnKRS();
            Load();
        }
        public void Load()
        {
            dwData = TBMain.NewRow();
            dwData["GSMANDANT"] = app.Mandant;
            dwData["GSART"] = ART;
            SResult = ((VGSDAT)MV).QueryGSDAT(dwData, -1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["DES"] = Helpers.Helper.GetGB2312Value(row["DES"].ToString());
            }
            MergeData(SResult, false);
        }

        public void Locate()
        {
            if (TBMain == null)
                return;
            if (TBMain.Rows.Count == 0)
                return;
            int idx = -1;
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["GSART"].ToString() + row["GSVAR"].ToString() == GSART
                        select row;
            foreach (DataRow row in query)
            {
                idx = TBMain.Rows.IndexOf(row);
                break;
            }
            vwMaster.View.MoveCurrentTo(idx);
        } 

    }
}
